package com.syuson.common.mybatis;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

/**
 * 
 * @author syuson
 *
 */
public class MyBatisGenerialDaoImpl implements MyBatisDao {

	private SqlSession sqlSession;

	public <E> List<E> findList(String namespace, String statementId, Map<String, Object> parameters) {
		return findLimitList(namespace, statementId, parameters, null);
	}

	public <E> List<E> findLimitList(String namespace, String statementId, Map<String, Object> parameters,
			Integer top) {
		String statement = namespace + "." + statementId;
		if (top != null) {
			RowBounds rowBounds = new RowBounds(0, top);
			return sqlSession.selectList(statement, parameters, rowBounds);
		} else {
			return sqlSession.selectList(statement, parameters);
		}
	}

	public <V> Map<String, V> findMap(String namespace, String statementId, Map<String, Object> parameters,
			String mapKey, Integer top) {
		String statement = namespace + "." + statementId;
		if (top != null) {
			RowBounds rowBounds = new RowBounds(0, top);
			return sqlSession.selectMap(statement, parameters, mapKey, rowBounds);
		} else {
			return sqlSession.selectMap(statement, parameters, mapKey);
		}
	}

	public <V> Map<String, V> findMap(String namespace, String statementId, Map<String, Object> parameters,
			String mapKey) {
		return findMap(namespace, statementId, parameters, mapKey, null);
	}

	public int insert(String namespace, String statementId, Map<String, Object> parameters) {
		String statement = namespace + "." + statementId;
		return sqlSession.insert(statement, parameters);
	}

	public int update(String namespace, String statementId, Map<String, Object> parameters) {
		String statement = namespace + "." + statementId;
		return sqlSession.update(statement, parameters);
	}

	public int delete(String namespace, String statementId, Map<String, Object> parameters) {
		String statement = namespace + "." + statementId;
		return sqlSession.delete(statement, parameters);
	}

	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}

}
